package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.BorrowLine;
import com.zyd.shiro.business.entity.RestoreLine;
import com.zyd.shiro.business.service.PakBorrowLineService;
import com.zyd.shiro.business.service.PakRestoreLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakBorrowLine;
import com.zyd.shiro.persistence.beans.PakRestoreLine;
import com.zyd.shiro.persistence.mapper.PakBorrowLineMapper;
import com.zyd.shiro.persistence.mapper.PakRestoreLineMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakRestoreLineServiceImpl implements PakRestoreLineService {

    @Autowired
    private PakRestoreLineMapper pakRestoreLineMapper;

    @Override
    public RestoreLine insert(RestoreLine restoreLine) {
        Assert.notNull(restoreLine, "对象不可为空！");
        restoreLine.getPakRestoreLine().setUpdateTime(new Date());
        restoreLine.getPakRestoreLine().setCreateTime(new Date());
        pakRestoreLineMapper.insertSelective(restoreLine.getPakRestoreLine());
        return restoreLine;
    }

    @Override
    public void insertList(List<RestoreLine> restoreLines) {
            for(RestoreLine restoreLine:restoreLines){
                restoreLine.getPakRestoreLine().setUpdateTime(new Date());
                restoreLine.getPakRestoreLine().setCreateTime(new Date());
                pakRestoreLineMapper.insertSelective(restoreLine.getPakRestoreLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakRestoreLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(RestoreLine restoreLine) {
        Assert.notNull(restoreLine, "对象不可为空！");
        restoreLine.setUpdateTime(new Date());
        return pakRestoreLineMapper.updateByPrimaryKey(restoreLine.getPakRestoreLine()) > 0;
    }

    @Override
    public boolean updateSelective(RestoreLine restoreLine) {
        Assert.notNull(restoreLine, "对象不可为空！");
        restoreLine.setUpdateTime(new Date());
        return pakRestoreLineMapper.updateByPrimaryKeySelective(restoreLine.getPakRestoreLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public RestoreLine getByPrimaryKey(Long primaryKey) {
        PakRestoreLine pakRestoreLine = pakRestoreLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakRestoreLine ? null : new RestoreLine(pakRestoreLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public RestoreLine getOneByEntity(RestoreLine restoreLine) {
        Assert.notNull(restoreLine, "对象不可为空！");
        PakRestoreLine pakRestoreLine = pakRestoreLineMapper.selectOne(restoreLine.getPakRestoreLine());
        return null == pakRestoreLine ? null : new RestoreLine(pakRestoreLine);
    }

    @Override
    public List<RestoreLine> listAll() {
        List<PakRestoreLine> pakRestoreLines = pakRestoreLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakRestoreLines)) {
            return null;
        }
        List<RestoreLine> restoreLines = new ArrayList<>();
        for (PakRestoreLine pakRestoreLine : pakRestoreLines) {
            restoreLines.add(new RestoreLine(pakRestoreLine));
        }
        return restoreLines;
    }

    @Override
    public List<RestoreLine> listByEntity(RestoreLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<RestoreLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakRestoreLine> pakRestoreLines = pakRestoreLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakRestoreLines)) {
            return null;
        }
        List<RestoreLine> restoreLines = new ArrayList<>();
        for (PakRestoreLine pakBorrowLine : pakRestoreLines) {
            restoreLines.add(new RestoreLine(pakBorrowLine));
        }
        PageInfo bean = new PageInfo<PakRestoreLine>(pakRestoreLines);
        bean.setList(restoreLines);
        return bean;
    }
    @Override
    public void increase(PakRestoreLine pakRestoreLine) {
        pakRestoreLineMapper.insert(pakRestoreLine);
    }

    @Override
    public List<PakRestoreLine> findByRestore(Long restoreId) {
        return pakRestoreLineMapper.findByRestore(restoreId);
    }


}
